// ignore_for_file: file_names, must_be_immutable, constant_identifier_names, slash_for_doc_comments, avoid_unnecessary_containers, prefer_const_constructors, unnecessary_cast, sized_box_for_whitespace, prefer_function_declarations_over_variables, overridden_fields, annotate_overrides, prefer_const_literals_to_create_immutables, unnecessary_null_comparison, deprecated_member_use, invalid_use_of_protected_member, unused_element, unused_local_variable, avoid_print

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yc_plugin_learn/base/abstract/IFFRefresh.dart';
import 'package:yc_plugin_learn/base/abstract/IFFScaffold.dart';
import 'package:yc_plugin_learn/base/tool/Animated/AnimatedSkeleton.dart';
import 'package:yc_plugin_learn/base/widget/layout/FFMoreBrach.dart';

/**页面父类，所有的页面都应该继承此类 */
class FFScaffold extends StatefulWidget {
  String? ffName;
  FFScaffold({Key? key, this.ffName}) : super(key: key);

  @override
  State<FFScaffold> createState() => FFScaffoldState();
}

class FFScaffoldState extends State<FFScaffold>
    with RouteAware, SingleTickerProviderStateMixin {
  /**页面基础控制 */
  late IFFScaffold iffScaffold;
  /**刷新控制 */
  IFFRefresh? iffRefresh;

  /**这次的状态与上次的状态是否改变了 */
  bool _statusChanged = false;
  /**上一次的状态 */
  FFPageStatus _lastPageStatus = FFPageStatus.firstLoading;

  @override
  void initState() {
    /**子类初始化方法 */
    iffScaffold.ffInitState();
    ffInitStateAfterLoadData();
    super.initState();
  }


  void ffInitStateAfterLoadData(){
    if (iffRefresh != null) {
      iffRefresh!.ffRefresh(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value:
          iffScaffold.ffStyle?.ffStatusTextColor ?? SystemUiOverlayStyle.dark,
      child: Scaffold(
        backgroundColor:
            iffScaffold.ffStyle?.ffStatusBackgroundColor ?? Colors.white,
        body: Column(
          children: [
            iffScaffold.ffNavi(context) ?? Container(),
            Expanded(
              child: FFMoreBrach(
                condition: iffScaffold.ffPageStatus,
                builderWidget: (BuildContext ctx, dynamic condition) {
                  //状态过渡有动画
                  Widget? thisWidget;
                  if (condition is FFPageStatus) {
                    if (_lastPageStatus != condition) {
                      _statusChanged = !_statusChanged;
                    }
                    if (condition == FFPageStatus.firstLoading) {
                      thisWidget = iffScaffold.ffFirstLoadingWidget(context);
                    } else if (condition == FFPageStatus.noData) {
                      thisWidget = iffScaffold.ffNoDataWidget(context);
                    } else if (condition == FFPageStatus.netError) {
                      thisWidget = iffScaffold.ffNetErrorWidget(context);
                    } else if (condition == FFPageStatus.result) {
                      thisWidget = _refreshWidget(
                          context, iffScaffold.ffBody(context) ?? Container());
                    }

                    _lastPageStatus = condition;
                  }

                  return Container(
                    width: MediaQuery.of(context).size.width,
                    color: iffScaffold.ffStyle?.ffBodyColor ?? Colors.white,
                    child: AnimatedSkeleton(
                      isLoading: _statusChanged,
                      skeletonWidget: thisWidget ?? Container(),
                      body: Container(
                        child: thisWidget,
                      ),
                    ),
                  );
                },
              ),
            ),
            iffScaffold.ffBottom(context) ?? Container(),
          ],
        ),
      ),
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute);
    iffScaffold.ffDidChangeDependencies();
  }

  /**页面销毁执行些方法 */
  @override
  void dispose() {
    print("页面销毁...");
    super.dispose();
    routeObserver.unsubscribe(this);
  }

  /**push到当前页面 */
  @override
  void didPush() {
    super.didPush();
  }

  /**当前页面push到下一页面 */
  @override
  void didPushNext() {
    super.didPushNext();
  }

  /**这个页面回到上个页面 */
  @override
  void didPop() {
    super.didPop();
  }

  /**回到这个页面 */
  @override
  void didPopNext() {
    super.didPopNext();
  }
}

/**刷新管理 */
extension RefreshManager on FFScaffoldState {
  Widget _refreshWidget(BuildContext ctx, Widget child) {
    if (iffRefresh != null) {
      return iffRefresh!.ffWidget(ctx, child);
    }
    return child;
  }
}
